package com.bw.liveplay.common.utils

import com.bw.liveplay.common.network.Api
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * @author KK
 * @description:
 * @date :2021/11/10 10:10
 *
 */
class ApiService private constructor(){
    companion object{
        private var apiService : ApiService ?=null
        get() {
            if(field == null){
                field = ApiService()
            }
            return field
        }

        fun instance():ApiService{
            return apiService!!
        }
    }

    //构建Retrofit对象用于发起网络请求
    fun getRetrofit():Retrofit?{

        val okBuilder : OkHttpClient.Builder = OkHttpClient.Builder()
        val loggingInterceptor : HttpLoggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
        okBuilder.addInterceptor(loggingInterceptor)
        okBuilder.readTimeout(30,TimeUnit.SECONDS)
        okBuilder.writeTimeout(30,TimeUnit.SECONDS)
        okBuilder.connectTimeout(30,TimeUnit.SECONDS)
        val build : Retrofit.Builder = Retrofit.Builder()
        build.client(okBuilder.build())
        build.baseUrl(Api.BASEURL)
        build.addConverterFactory(GsonConverterFactory.create())
        build.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        return build.build()
    }

}